﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FAlbumWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FAlbumWP";
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FAlbumWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "SOYT-Album ảnh hiển thị trang chủ");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                        new ViewCls("Action",GetText("Action","Ảnh cổ động bên phải"),0),
                    new ViewCls("Right",GetText("Right","Album ảnh hoạt động"),1),              
                    new ViewCls("RightLink",GetText("RightLink","Ảnh liên kết bên phải"),2),   
                    new ViewCls("Video",GetText("Video","Thư viện video"),3),   
                      new ViewCls("Function",GetText("Function","Ảnh liên kết chức năng"),4),   
                };
            }
        }


        private string ClientRenderWebPartRightFunction(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            StringBuilder Html = new StringBuilder();
            Html.Append("<div class=\"function-banner\">\n");

            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                string Url = "";
                try
                {
                    Url = (string)dtItems.Rows[iIndex]["Url"];
                }
                catch { Url = "#"; }
                Html.Append("<a href=\"" + Url + "\"><img src=\"" + ImageUrl + "\" /></a>");
            }
            Html.Append("</div>");

            return Html.ToString();
        }


        private string ClientRenderWebPartRightLink(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            StringBuilder Html = new StringBuilder();
            Html.Append("<div class=\"adv-group\">\n");

            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                string Url = "";
                try
                {
                    Url = (string)dtItems.Rows[iIndex]["Url"];
                }
                catch { Url = "#"; }
                Html.Append("<a href=\"" + Url + "\"><img src=\"" + ImageUrl + "\" /></a>");
            }
            Html.Append("</div>");

            return Html.ToString();
        }

        private string ClientRenderWebPartRightVideo(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            StringBuilder Html = new StringBuilder();



            string AdvImageItemId = (string)dtItems.Rows[0]["AdvImageItemId"];
            string FileImage = (string)dtItems.Rows[0]["FileImage"];
            string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
            string Url = "";
            try
            {
                Url = (string)dtItems.Rows[0]["Url"];
            }
            catch { Url = "#"; }

            string Description = "";
            try
            {
                Description = (string)dtItems.Rows[0]["Description"];
            }
            catch { Description = ""; }

            Html.Append(
                "<div class=\"video-hompage\">\n" +
                "	<div class=\"side-homepage-header1\">\n" +
                "		<h2><a href=\"" + Url + "\">THƯ VIỆN VIDEO</a></h2>\n" +
                "		<a href=\"" + Url + "\">xem toàn bộ &raquo</a>\n" +
                "	</div>\n"
                );
            Html.Append(
                "	<div class=\"video-homepage-content\">\n" +
            "		<object class=\"videoplayer\" classid=\"clsid:EF85ADB4-5D94-48F4-8343-839F9EEFF887\" width=\"100%\"  id=\"player1\" name=\"player1\">\n" +
            "			<param name=\"movie\" value=\"/js/player.swf\">\n" +
            "			<param name=\"allowfullscreen\" value=\"true\">\n" +
            "			<param name=\"menu\" value=\"false\">\n" +
            "			<param name=\"allowscriptaccess\" value=\"always\">\n" +
            "			<param name=\"flashvars\" value=\"file=medias/video.flv;image=/medias/videothumb.jpg\">\n" +
            "			<embed id=\"EF85ADB4-5D94-48F4-8343-839F9EEFF887\" name=\"player1\" src=\"js/player.swf\" width=\"100%\"  allowscriptaccess=\"always\" menu=\"false\" allowfullscreen=\"true\" flashvars=\"file=" + ImageUrl + "&amp;image=/NewsImage/_t/blank1_7beaa243-e7a2-4431-93da-b684ae59079a_jpg.jpg\">\n" +
                //"			<embed id=\"EF85ADB4-5D94-48F4-8343-839F9EEFF887\" name=\"player1\" src=\"js/player.swf\" width=\"100%\"  allowscriptaccess=\"always\" menu=\"false\" allowfullscreen=\"true\" flashvars=\"file=/Portal_Audios_Attachments/QNgai.flv&amp;image=/NewsImage/_t/blank1_7beaa243-e7a2-4431-93da-b684ae59079a_jpg.jpg\">\n" +
            "		</object>\n" +
            "		<p class=\"video-title\">"+Description+"</p>\n" +
            "	</div>\n"
                );

            Html.Append("</div>");

            return Html.ToString();
        }


        private string ClientRenderWebPartAction(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            StringBuilder Html = new StringBuilder();
            StringBuilder olHtml = new StringBuilder();
            StringBuilder divHtml = new StringBuilder();

            Html.Append("<div id=\"myCarousel\" class=\"carousel slide\">\n");
            divHtml.Append("			<div class=\"carousel-inner\">\n");
            olHtml.Append(
                "	<ol class=\"carousel-indicators\">\n");

            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                string Url = "";
                try
                {
                    Url = (string)dtItems.Rows[iIndex]["Url"];
                }
                catch { Url = "#"; }
                string _class = "";
                string _divClass = "";
                if (iIndex == 0)
                {
                    _class = "class=\"active\"";
                    _divClass = "active";
                }

                olHtml.Append(" <li data-target=\"#myCarousel\" data-slide-to=\"0\" " + _class + "></li>\n");
                divHtml.Append("    <div class=\"item " + _divClass + "\">\n" +
                                "	    <img src=\"" + ImageUrl + "\" alt=\"\">\n" +
                                "    </div>\n");

            }
            olHtml.Append("</ol>\n");
            divHtml.Append("</div>\n");
            Html.Append(olHtml.ToString() + divHtml.ToString());
            Html.Append("</div>");

            return Html.ToString();
        }


        private string ClientRenderWebPartRight(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            try
            {
                StringBuilder Html = new StringBuilder();
                DataTable dtItems = new FAlbumBLL().ReadingAlbumDefaulView();

                int ICount = 0;
                try
                {
                    ICount = int.Parse(Environment.Environments.GetAppSettings("ImgInHome"));
                }
                catch { ICount = 4; }

                
                if (dtItems.Rows.Count < ICount)                
                {
                    ICount = dtItems.Rows.Count;
                }
                Html.Append(
                   "<div class=\"image-hompage\">\n" +
                    "					<div class=\"side-homepage-header1\">\n" +
                    "						<h2><a href=\"AlbumList.aspx\">HÌNH ẢNH HOẠT ĐỘNG</a></h2>\n" +
                    "					</div>\n");


                StringBuilder SubHtml = new StringBuilder();
                SubHtml.Append(
                    "   <div class=\"image-homepage-content\">\n" +
                    "       <ul id=\"mycarousel2\" class=\"jcarousel-skin-tango\">\n");
                for (int iIndex = 0; iIndex < ICount; iIndex++)
                {
                    string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                    string AdvImageItemName = (string)dtItems.Rows[iIndex][belongSite + "AdvImageItemName"];
                    string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                    string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/crop/" + FileImage;
                    string Url = "";
                    try
                    {
                        Url = Ultility.GetAlbumDetails(belongSite, TabModuleCls.Title, AdvImageId.Substring(0, 8), "picture");
                    }
                    catch { Url = "#"; }

                    SubHtml.Append("<li><a href=\"" + Url + "\"><img src=\"" + ImageUrl + "\" alt=\"\" /><span class=\"documentTitle\">" + AdvImageItemName + "</span></a></li>\n");
                }
                SubHtml.Append("</ul></div>");
                Html.Append(SubHtml.ToString() +
                    "</div>");

                return Html.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {

                if (ViewName.Equals("Action"))
                {
                    return ClientRenderWebPartAction(TabModuleId, HasPorlet);
                }

                if (ViewName.Equals("Right"))
                {
                    return ClientRenderWebPartRight(TabModuleId, HasPorlet);
                }
                if (ViewName.Equals("RightLink"))
                {
                    return ClientRenderWebPartRightLink(TabModuleId, HasPorlet);
                }
                if (ViewName.Equals("Video"))
                {
                    return ClientRenderWebPartRightVideo(TabModuleId, HasPorlet);
                }

                if (ViewName.Equals("Function"))
                {
                    return ClientRenderWebPartRightFunction(TabModuleId, HasPorlet);
                }



                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }



        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            string Query = "select Id=AdvImageId,Subject=[" + ActiveLanguage + "Subject] from TableAdvImage where belongSite='" + belongSite + "'";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FAlbumWP), ActivePage);
        }
    }
}
